package com.no1.zy.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.no1.zy.mapper.UserMapper;
import com.no1.zy.pojo.User;
import com.no1.zy.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Override
    @Transactional(propagation = Propagation.REQUIRED, readOnly = true)
    public User queryById(Long id) {
        return userMapper.selectByPrimaryKey(id);
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = {Exception.class})
    public void modifyUser(User user) {
        userMapper.updateByPrimaryKeySelective(user);
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = {Exception.class})
    public void saveUser(User user) {
        userMapper.insertSelective(user);

    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED, readOnly = true)
    public List<User> queryAll() {
        return userMapper.selectAll();
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = {Exception.class})
    public void removeUser(Long id) {
        userMapper.deleteByPrimaryKey(id);

    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED, readOnly = true)
    public PageInfo<User> queryByPage(Integer curPage, Integer pageRecords) {
        PageHelper.startPage(curPage,pageRecords);
        List<User> userList=userMapper.selectAll();
        return new PageInfo<User>(userList);
    }
}
